/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Config;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 *
 * @author HungVM_C00359
 */
public class Localization {

    private static Localization instance;
    private String message;

    public Localization() {
        if(instance !=null){

        }
    }

    public static Localization getInstance(){
        if(instance ==null){
                instance = new Localization();
        }

        return instance;
    }
    
    public static String getMessage(String msg){
        try {
              File file = new File("src\\Config\\localization.xml");
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();

              org.w3c.dom.Document doc = db.parse(file);
              doc.getDocumentElement().normalize();
              System.out.println("Root element " + doc.getDocumentElement().getNodeName()); //phong kham
              NodeList nodeLst = doc.getElementsByTagName("Message");

              for (int s = 0; s < nodeLst.getLength(); s++) {
                  Node fstNode = (Node) nodeLst.item(s);
                  if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                      Element fstElmnt = (Element) fstNode;
                      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("String");
                      for(int i=0;i<fstNmElmntLst.getLength();i++){
                           Element elemt = (Element) fstNmElmntLst.item(i);
                           NodeList fst = elemt.getChildNodes();
                           if(elemt.getAttribute("id").equals(msg)){
                               return ((Node) fst.item(0)).getNodeValue();
                           }
                      }
//                      for(int i=0;i<fstNmElmntLst.getLength();i++){
//                          Node nodeStr = (Node) fstNmElmntLst.item(i);
//                          if(nodeStr.getNodeType() == Node.ELEMENT_NODE){
//                              Element eleStr = (Element)nodeStr;
//
//                              if(eleStr.getAttribute("id").equals(msg)){
//                                  System.out.println(nodeStr.getNodeValue());
//                                  return nodeStr.getNodeValue();
//                              }
//                          }
//                      }
                   }
              }
          } catch (Exception e) {
            e.printStackTrace();
          }
        return "Error, cannot find message";
    }



}
